package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCopyrightAlreadyPublishBinding;
import com.sgb.kjwl.model.entity.enterprise.PublishCopyrightDetailEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverPublishActivity;
import com.sgb.kjwl.viewmodel.enterprise.PublishViewModel;

public class CopyrightAlreadyPublishFragment extends AppFragment {

    FragmentCopyrightAlreadyPublishBinding mBinding;
    PublishViewModel mViewModel;
    PublishCopyrightDetailEntity mEntity;

    public static CopyrightAlreadyPublishFragment newInstance(long id) {
        CopyrightAlreadyPublishFragment tradePublishFragment = new CopyrightAlreadyPublishFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("id", id);
        tradePublishFragment.setArguments(bundle);
        return tradePublishFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_copyright_already_publish, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(PublishViewModel.class);
        long id = getArguments().getLong("id");
        mBinding.setViewModel(mViewModel);
        mBinding.setContext(getContext());
        mBinding.tvEnable.setOnClickListener(this);
        mBinding.tvOperateDown.setOnClickListener(this);
        mBinding.tvDelete.setOnClickListener(this);

        mViewModel.getCopyrightPublishDetail(getContext(), id + "", 3);
        mViewModel.mJump.observe(this.getViewLifecycleOwner(), integer -> {
            if (integer == 3) {
                ActivityJump.jumpToActivityForResult(getActivity(), MakeOverPublishActivity.class, 1, Pair.create("type", integer), Pair.create("entity", mEntity));
            }
        });

        mViewModel.mDeleteSecess_Type.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                getActivity().finish();
                ;
            }
        });

        mViewModel.mPublishCopyrightDetailLiveData.observe(getViewLifecycleOwner(), new Observer<PublishCopyrightDetailEntity>() {
            @Override
            public void onChanged(PublishCopyrightDetailEntity publishCopyrightDetailEntity) {
                mBinding.idRootLayout.setVisibility(View.VISIBLE);
                mBinding.setEntity(publishCopyrightDetailEntity);
                mEntity = publishCopyrightDetailEntity;
                //在售中
                if (null == mEntity.getOrderStatus() || 3 == mEntity.getOrderStatus()) {
                    mBinding.tvOperateDown.setVisibility(1 == mEntity.getStatus() ? View.VISIBLE : View.GONE);
                    mBinding.llBottom.setVisibility(0 == mEntity.getStatus() ? View.VISIBLE : View.GONE);
                } else if (1 == mEntity.getOrderStatus() || 2 == mEntity.getOrderStatus() || 4 == mEntity.getOrderStatus()) {
                    //交易中
                    mBinding.llBottom.setVisibility(View.GONE);
                    mBinding.tvOperateDown.setVisibility(View.GONE);
                } else if (5 == mEntity.getOrderStatus()) {
                    //已出售
                    mBinding.tvOperateDown.setVisibility(View.GONE);
                    mBinding.llBottom.setVisibility(View.GONE);
                }
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_enable://上架
                if (mEntity != null) {
                    DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认上架？", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            mViewModel.changeStateMakeoverPublish(getContext(), mEntity.getId(), 3, "1");
                        }
                    });
                }
                break;
            case R.id.tv_operate_down://下架
                if (mEntity != null) {
                    DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认下架？", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            mViewModel.changeStateMakeoverPublish(getContext(), mEntity.getId(), 3, "0");
                        }
                    });
                }
                break;
            case R.id.tv_delete://删除
                DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认删除？", "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        mViewModel.deleteMakeoverPublish(getContext(), mEntity.getId() + "", 3);
                    }
                });
                break;
        }
    }

}
